package com.mmall.util;

import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

public class FtpUtil {

    private static final Logger logger= LoggerFactory.getLogger(FtpUtil.class);
    private static String ftpIp = PropertiesUtil.getProperty("ftp.server.ip");
    private static String ftpUser = PropertiesUtil.getProperty("ftp.user");
    private static String ftpPass = PropertiesUtil.getProperty("ftp.pass");

    public FtpUtil(String ip,int port,String user,String pass){
        this.ip = ip;
        this.port = port;
        this.user = user;
        this.pass = pass;
    }

    public static Boolean uploadFile(List<File> fileList) throws IOException {
        FtpUtil ftpUtil = new FtpUtil(ftpIp,22,ftpUser,ftpPass);
        logger.info("开始链接服务器");
        boolean result = ftpUtil.uploadFile("img",fileList);
        logger.info("开始链接服务器,结束上传,上传结果:{}",result);
        return result;
    }

    private boolean uploadFile(String remotePath,List<File> fileList) throws IOException {
        boolean uploaded = true;
        FileInputStream fileInputStream = null;
        // 链接ftp服务器
        if (connerServer(this.ip,this.port,this.user,this.pass)){
            try {
                // 路径
                ftpClient.changeWorkingDirectory(remotePath);
                // 大小
                ftpClient.setBufferSize(1024);
                // 格式
                ftpClient.setControlEncoding("UTF-8");
                // 二进制
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                ftpClient.enterLocalPassiveMode();
                for (File fileItem:
                     fileList) {
                    // 转换
                    fileInputStream = new FileInputStream(fileItem);
                    // 导入input流
                    ftpClient.storeFile(fileItem.getName(),fileInputStream);
                }
            } catch (IOException e) {
                uploaded = false;
                logger.error("上传文件异常",e);
            } finally {
                // 关闭流
                fileInputStream.close();
                // 释放
                ftpClient.disconnect();
            }
        }
        return uploaded;
    }

    private boolean connerServer(String ip,int port,String user,String pass){
        boolean isSuccess = false;
        ftpClient = new FTPClient();
        try {
            ftpClient.connect(ip);
            isSuccess = ftpClient.login(user,pass);
        } catch (IOException e) {
            logger.error("ftp图片服务器链接异常",e);
        }
        return isSuccess;
    }


    private String ip;
    private int port;
    private String user;
    private String pass;
    private FTPClient ftpClient;

    public static String getFtpIp() {
        return ftpIp;
    }

    public static void setFtpIp(String ftpIp) {
        FtpUtil.ftpIp = ftpIp;
    }

    public static String getFtpUser() {
        return ftpUser;
    }

    public static void setFtpUser(String ftpUser) {
        FtpUtil.ftpUser = ftpUser;
    }

    public static String getFtpPass() {
        return ftpPass;
    }

    public static void setFtpPass(String ftpPass) {
        FtpUtil.ftpPass = ftpPass;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public FTPClient getFtpClient() {
        return ftpClient;
    }

    public void setFtpClient(FTPClient ftpClient) {
        this.ftpClient = ftpClient;
    }
}
